package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.edu.bean.*;
import com.zy.edu.config.SystemParam;
import com.zy.edu.dao.CourseChapterVideoStuDao;
import com.zy.edu.service.CourseChapterVideoStuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-07-27
 */
@Service
public class CourseChapterVideoStuServiceImpl extends ServiceImpl<CourseChapterVideoStuDao, CourseChapterVideoStu> implements CourseChapterVideoStuService {

    @Override
    public Map saveOrUpdate(CourseChapterVideoStu courseChapterVideoStu, InputParam inputParam) {
        if(StringUtils.isEmpty(courseChapterVideoStu.getVideoId())
            ||StringUtils.isEmpty(courseChapterVideoStu.getUserId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }

        EntityWrapper<CourseChapterVideoStu> temp = new EntityWrapper<>();
        temp.eq("video_id",courseChapterVideoStu.getVideoId())
                .eq("user_id",courseChapterVideoStu.getUserId());

        List<CourseChapterVideoStu> list = this.baseMapper.selectList(temp);

        if (list.size()==0) {
            courseChapterVideoStu.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            courseChapterVideoStu.setCreateTime(new Date());
            courseChapterVideoStu.setFinished(0);
            this.baseMapper.insert(courseChapterVideoStu);
        } else {
            courseChapterVideoStu.setId(list.get(0).getId());
            //需要记录上次观看的时间点
            this.baseMapper.updateById(courseChapterVideoStu);
        }
        return ResponseInfo.success(null);
    }

    @Override
    public Map dele(CourseChapterVideoStu courseChapterVideoStu) {
        if(StringUtils.isEmpty(courseChapterVideoStu.getVideoId())
                ||StringUtils.isEmpty(courseChapterVideoStu.getUserId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
        this.baseMapper.delete(new EntityWrapper<CourseChapterVideoStu>()
        .eq("video_id",courseChapterVideoStu.getVideoId())
                .eq("user_id",courseChapterVideoStu.getUserId()));
        return ResponseInfo.success(null);
    }

    @Override
    public Map info(CourseChapterVideoStu courseChapterVideoStu, InputParam inputParam) {

        if(StringUtils.isEmpty(courseChapterVideoStu.getVideoId())
                ||StringUtils.isEmpty(courseChapterVideoStu.getUserId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }

        EntityWrapper<CourseChapterVideoStu> temp = new EntityWrapper<>();
        temp.eq("video_id",courseChapterVideoStu.getVideoId())
                .eq("user_id",courseChapterVideoStu.getUserId());

        List<CourseChapterVideoStu> list = this.baseMapper.selectList(temp);
        if (list.size()>0) {
            return ResponseInfo.success(list.get(0));
        }
         return ResponseInfo.success(null);
    }
}
